package com.hdjk.view.callback;

import com.hdjk.view.entity.BuyPlanExecuteHistoryEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

/**
 * Created by sxy on 2017/11/2.
 */

public abstract class BuyPlanExecuteHistoryCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg = super.parseNetworkResponse(response, i);
        JSONArray array = (JSONArray) msg.getData();
        if (array != null) {
            JSONObject jsonObject = array.getJSONObject(0);
            BuyPlanExecuteHistoryEntity entity = new BuyPlanExecuteHistoryEntity();
            if (!jsonObject.isNull("merchantNumber")) {
                entity.setMerchantNumber(jsonObject.getString("merchantNumber"));
            }
            if (!jsonObject.isNull("allTransactionAmount")) {
                double l = jsonObject.getDouble("allTransactionAmount");
                BigDecimal b = new BigDecimal(l);
                entity.setAllTransactionAmount(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            }
            if (!jsonObject.isNull("products")) {
                JSONArray jsonArray = jsonObject.getJSONArray("products");
                List<BuyPlanExecuteHistoryEntity.Products> productses = new ArrayList<>();
                if (jsonArray != null) {
                    for (int n = 0; n < jsonArray.length(); n++) {
                        BuyPlanExecuteHistoryEntity.Products products = new BuyPlanExecuteHistoryEntity().new Products();
                        JSONObject json = jsonArray.getJSONObject(n);
                        if (!json.isNull("fundCode")) {
                            products.setFundCode(json.getString("fundCode"));
                        }
                        if (!json.isNull("fundName")) {
                            products.setFundName(json.getString("fundName"));
                        }
                        if (!json.isNull("contractNumber")) {
                            products.setContractNumber(json.getString("contractNumber"));
                        }
                        if (!json.isNull("orderDate")) {
                            products.setOrderDate(json.getString("orderDate"));
                        }
                        if (!json.isNull("transactionDate")) {
                            products.setTransactionDate(json.getString("transactionDate"));
                        }
                        if (!json.isNull("transactionStatus")) {
                            products.setTransactionStatus(json.getString("transactionStatus"));
                        }
                        if (!json.isNull("transactionAmount")) {
                            double l = json.getDouble("transactionAmount");
                            BigDecimal b = new BigDecimal(l);
                            products.setTransactionAmount(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                        }
                        productses.add(products);
                    }
                }
                entity.setProducts(productses);
            }
            msg.setData(entity);
        }
        return msg;
    }
}
